package cn.com.jsepc.rep.catalog;

import cn.com.jsepc.rep.comm.Common;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
import cn.com.jsepc.rep.comm.ConstUtil;
import cn.com.jsepc.rep.demension.CheckLeaf;
import cn.com.jsepc.rep.demension.DementionDAO;
import cn.com.jsepc.rep.vo.DementionVO;
import javax.servlet.ServletException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.com.jsepc.rep.vo.ReportVO;
import cn.com.jsepc.rep.vo.TreeVO;

import com.jarp.db.DAO;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CatalogDAO {
     private Boolean isLeaf;
	public String getCatalog()
    {
        String res = "";        
        res= this.getData("TB_CATALOG", "PARENTCATAID", "0");
        return res;
    }
        
        public String getData(String table,String parentcol,String parentid) {
        String res = "";

        /*
         * 这里是为了获取Demention表中字段名
         */

        try {
            DAO dao = new DAO();
            List<TreeVO> lstResults = new ArrayList<TreeVO>();   
            String sql = "SELECT * FROM TB_CATALOG where  "+parentcol+"="+parentid;
            Vector v = dao.getDataSet(ConstUtil.DBNAME, sql);
            
            if (v != null) {
                for (int i = 0; i < v.size(); i++) {
                    Hashtable hashRows = (Hashtable) v.get(i);
                    TreeVO treeVO = new TreeVO();
                    treeVO.setId((String) hashRows.get("CATAID"));
                    treeVO.setText((String) hashRows.get("CATANAME"));
                    treeVO.setparentCataID((String) hashRows.get("PARENTCATAID"));
                    treeVO.setcataNO((String) hashRows.get("CATANO"));
                    isLeaf=new CheckLeaf().isLeaf(treeVO.getId(), "PARENTCATAID", "TB_CATALOG");
                    treeVO.setLeaf(isLeaf);
                    if(isLeaf==false){
                       treeVO.setChildren( this.getData(table, parentcol, treeVO.getId()));
                    }
                    treeVO.setCheck(false);
                    lstResults.add(treeVO);
                }
                JSONArray json = new JSONArray();
                json = JSONArray.fromObject(lstResults);
                res = json.toString();

            }

        } catch (ServletException ex) {
            Logger.getLogger(DementionDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }
 
	
	public String addCatalog(String itemJsonInfo){
		JSONObject jsonObject = new JSONObject(itemJsonInfo);
		ReportVO vo = new ReportVO();
		String CATAID=new Common().getSeqStr("SEQ_CATALOG");
		String CATANAME=jsonObject.getString("CATANAME");
		String PARENTCATAID=jsonObject.getString("PARENTCATAID");
		String CATANO=jsonObject.getString("CATANO");
		 DAO dao = new DAO();
		String sql="insert into TB_CATALOG(CATAID,CATANAME,CATANO,PARENTCATAID) values("+CATAID+",'"+CATANAME+"','"+CATANO+"',"+PARENTCATAID+")";
		
		try {
			int i=dao.execCommand(ConstUtil.DBNAME, sql);
			if(i!=0)
                        return "ok";
                        else 
                            return "wrong";
		} catch (ServletException e) {
			e.printStackTrace();
                        return "wrong";
		}
                
	}
        
        public String updateCatalog(String itemJsonInfo){
                DAO dao = new DAO();
		JSONObject jsonObject = new JSONObject(itemJsonInfo);
		ReportVO vo = new ReportVO();
                String CATANAME=jsonObject.getString("CATANAME");
		String PARENTCATAID=jsonObject.getString("PARENTCATAID");
		String CATANO=jsonObject.getString("CATANO");
                String CATAID="";
                String cataid_sql="SELECT cataid from tb_catalog where cataname='"+CATANAME+"' or CATANO='"+CATANO+"'";
        try {
            Vector v=dao.getDataSet(ConstUtil.DBNAME, cataid_sql);
            Hashtable hashRows = (Hashtable) v.get(0);
            CATAID=(String)hashRows.get("CATAID");
        } catch (ServletException ex) {
            Logger.getLogger(CatalogDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
		
		//String sql="insert into TB_CATALOG(CATAID,CATANAME,CATANO,PARENTCATAID) values("+CATAID+",'"+CATANAME+"','"+CATANO+"','"+PARENTCATAID+"')";
		String sql="update TB_CATALOG set CATANAME='"+CATANAME+"',CATANO='"+CATANO+"' where CATAID="+CATAID;
                 try {
			int i=dao.execCommand(ConstUtil.DBNAME, sql);
			if(i!=0)
                        return "ok";
                        else 
                            return "wrong";
		} catch (ServletException e) {
			e.printStackTrace();
                        return "wrong";
		}
                
	}
	
	public String deleteCatalog(String itemJsonInfo){
		JSONObject jsonObject = new JSONObject(itemJsonInfo);
                String CATAID=jsonObject.getString("CATAID");
		DAO dao = new DAO();
		String sql="delete  from TB_CATALOG where CATAID="+CATAID;
		String sql2="delete from TB_CATALOG where parentcataid="+CATAID;
		try {
			int iRows=dao.execCommand(ConstUtil.DBNAME, sql);
			if(iRows>0){
			return "ok";
			}
			else
				return "wrong";
		} catch (ServletException e) {
			e.printStackTrace();
		}
		return "wrong";
	}
	
	public String getChildrenCataID(String pcataid){
		StringBuffer str = new StringBuffer();
		try {
			DAO dao = new DAO();
			String sql = "SELECT CATAID FROM TB_CATALOG where  PARENTCATAID = "+pcataid;
			Vector v = dao.getDataSet(ConstUtil.DBNAME, sql);
			if (v != null) {
                for (int i = 0; i < v.size(); i++) {
                    Hashtable hashRows = (Hashtable) v.get(i);
                    String sid = (String) hashRows.get("CATAID");
                    str.append(","+sid);
                    str.append(getChildrenCataID(sid));
               }
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str.toString();
	}
	
	public String getAllCatatID(String pcataid){
		StringBuffer str = new StringBuffer();
		String str1 = getChildrenCataID(pcataid);
		try {
			str.append(pcataid);
			str.append(str1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str.toString();
		
	}
	
}
